// 栈和十进制转二进制算法

function Stack() {
    // 栈的属性
    this.items = [];
    // 栈的相关操作
    // 1、元素压栈
    Stack.prototype.push = function(element) {
        this.items.push(element);
    };
    // 2、元素弹栈
    Stack.prototype.pop = function() {
        return this.items.pop();
    };
    // 3、返回元素栈顶元素
    Stack.prototype.peek = function() {
        return this.items[this.items.length - 1];
        // 4、判断栈是否为空
    };
    Stack.prototype.isEmpty = function() {
        return this.items.length === 0;
    };
    // 5、获取栈中元素的个数
    Stack.prototype.size = function() {
        return this.items.length;
    };
    // 6、toString方法
    Stack.prototype.toString = function() {
        var resultString = '';
        for (var i = 0; i < this.items.length; i++) {
            resultString += this.items[i] + ' ';
        }
        return resultString;
    };
}

// 栈的使用
var s = new Stack();
s.push(20);

// 十进制变二进制
function dec2bin(decNumber) {
    var stack = new Stack();
    while (decNumber > 0) {
        stack.push(decNumber % 2);
        decNumber = Math.floor(decNumber / 2);
    }
    var binaryString = '';
    while (!stack.isEmpty()) {
        binaryString += stack.pop();
    }
    return binaryString;
}

// 测试dec2bin函数
alert(dec2bin(100));
alert(dec2bin(10));